package com.zhoux.mvc.action;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ActionListener implements ServletContextListener {
	public static List<String> list = new ArrayList<String>();

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("注销Servlet容器！");

	}

	/**
	 * 以注解的0方式加载Servlet的配置
	 */
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
		Map<String, String> configMap = new HashMap<String, String>();
		ServletContext context = arg0.getServletContext();
		
		String path = context.getRealPath("\\")+"WEB-INF"+File.separator+"classes"+File.separator;
		String packag = "com.zhoux.mvc.test".replace(".", File.separator);
		System.out.println(path + packag);
		File file = new File(path + packag);
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for (File file2 : files) {
				getfileName(file2,path);
			}
		}else{
			list.add(file.getPath().substring(path.length()).split("\\.")[0].replace(File.separator, "."));
		}
		
		// 通过反射获取扫描所有文件中被注解的类
		for (int i = 0; i < list.size(); i++) {
			Class clazz = null;
			try {
				clazz = Class.forName(list.get(i));
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				System.err.println(list.get(i) + " is not exits");
			}
			ActionAnnotation annotation = (ActionAnnotation) clazz.getAnnotation(ActionAnnotation.class);
			if (annotation == null)
				continue;
			configMap.put(annotation.RequestMapping(), list.get(i));
			System.out.println("请求的地址：" + annotation.RequestMapping() + "；处理对应请求的类：" + list.get(i));
		}

		context.setAttribute("configMap", configMap);

		System.out.println(context.getContextPath());

		System.out.println("所有处理请求的接口已经加载完成。。。");

	}

	/**
	 * 遍历项目下的所有java文件
	 * 
	 * @param file
	 */
	public static void getfileName(File file, String rootPath) {
		if (!file.isDirectory()) {
			System.out.println("文件的路径：" + file.getPath() + "文件名：" + file.getName());
			list.add(file.getPath().substring(rootPath.length()).split("\\.")[0].replace(File.separator, "."));
			return;
		}
		File[] files = file.listFiles();
		for (File f : files) {
			getfileName(f, rootPath);
		}

	}

	/**
	 * 以xml的方式接受请求
	 */
	// @Override
	// public void contextInitialized(ServletContextEvent arg0) {
	// // TODO Auto-generated method stub
	// System.out.println("开始加载mvc配置文件");
	// ServletContext context=arg0.getServletContext();
	// String xmlPath=context.getInitParameter("mvc-config");
	// String realPath=context.getRealPath("\\");
	// System.out.println(realPath+xmlPath);
	// Map<String, XmlBean> map = MvcXmlUtil.jiexi(realPath+xmlPath);
	// context.setAttribute("mvc", map);
	// System.out.println("加载配置文件完成！");
	//
	// }

	public static void main(String[] args) {
		System.out.println(".");
		"com.zhoux.mvc.test".trim().replace(".", File.separator);
		System.out.println("com.zhoux.mvc.test".trim().replace(".", File.separator));
	}
}
